package com.szewec.ps.ui.member;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.szewec.ps.R;
import com.szewec.ps.base.BaseActivity;
import com.szewec.ps.databinding.ActivityMemberSearchBinding;
import com.szewec.ps.model.user.UserModel;
import com.szewec.ps.util.TextViewUtil;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

public class MemberSearchActivity extends BaseActivity {

    private ActivityMemberSearchBinding _binding;

    private List<UserModel> _memeberModelList = new ArrayList<>();

    private MemberListAdapter _memberListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        _binding = DataBindingUtil.setContentView(this, R.layout.activity_member_search);
        ButterKnife.bind(this);


        LinearLayoutManager manager0 = new LinearLayoutManager(this);
        manager0.setOrientation(LinearLayoutManager.VERTICAL);
        _binding.memberRecyclerView.setLayoutManager(manager0);
        _memberListAdapter = new MemberListAdapter(this);
        _binding.memberRecyclerView.setAdapter(_memberListAdapter);

        //设置输入框获取焦点并弹出键盘
        _binding.searchTitleBar.setEtSearchFocus();
        //点击取消按钮返回
        _binding.searchTitleBar.setRightLayoutClickListener(this::returnBack);
        //搜索框文本监听
        _binding.searchTitleBar.setTextChangedListener(this::setSearchListener);
        //软件盘点击搜索跳转搜索结果页面
        _binding.searchTitleBar.setEditorActionListener(this::setEditorActionListener);
    }

    /**
     * 点击取消按钮返回列表页面
     */
    private void returnBack(){
        TextViewUtil.closeKeyboard(this);
        finish();
    }

    /**
     * 初始化view的监听事件， 如搜索框的文本事件,软键盘的点击事件
     */
    private void setSearchListener() {
    }

    /**
     * 软键盘搜索事件
     */
    private boolean setEditorActionListener(TextView textView, int i, KeyEvent keyEvent) {
        if(i == EditorInfo.IME_ACTION_SEARCH || i == EditorInfo.IME_ACTION_UNSPECIFIED){
            String code = textView.getText().toString();
            if (!TextUtils.isEmpty(code)) {
                /*linkToSearchResultActivity(code);*/
            }
            return true;
        }
        return false;
    }


    class MemberListAdapter extends RecyclerView.Adapter<MemberListAdapter.MemberListViewHolder>{

        private Context context;

        public MemberListAdapter(Context context){
            this.context = context;
        }

        @Override
        public int getItemCount() {
            return 10;
        }

        @Override//getItemViewType返回行号的化，onCreateViewHolder的viewType就是行号
        public int getItemViewType(int position) {
            return position;
        }

        @NotNull
        @Override
        public MemberListViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
            //获取添加视图
            View contentView = LayoutInflater.from(context).inflate(R.layout.item_member_search_cell, parent, false);
            return new MemberListViewHolder(contentView);
        }


        @Override
        public void onBindViewHolder(@NotNull MemberSearchActivity.MemberListAdapter.MemberListViewHolder viewHolder, int position) {
            viewHolder.btnAddMember.setOnClickListener((view)->{
                finish();
            });
        }


        //内部类，绑定控件
        class MemberListViewHolder extends RecyclerView.ViewHolder{
            Button btnAddMember;
            public MemberListViewHolder(View itemView) {
                super(itemView);
                btnAddMember = itemView.findViewById(R.id.btn_Add_Member);
            }
        }
    }
}
